package com.tgy.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/** @Author: tgy @Date: 3/11/21 11:07 AM */
public class _227_基本计算器_II {

    public static int calculate(String s){

        Stack<Integer> stack = new Stack<>();
        Stack<Character> opStack = new Stack<>();

        char[] chars = s.toCharArray();

        StringBuilder sb = new StringBuilder();

        for (char aChar : chars) {

            if (aChar >='0' && aChar <='9') {

                sb.append(aChar);
            }else if (aChar == '+' || aChar == '-' || aChar == '*' || aChar == '/') {

                stack.push(Integer.valueOf(sb.toString()));
                sb.delete(0,sb.length());
                while (!opStack.isEmpty() && !((opStack.peek() == '-' || opStack.peek() == '+') && (aChar == '*' || aChar == '/'))) {

                    int v1  = stack.pop();
                    int v2 = stack.pop();

                    int val = 0;
                    Character op = opStack.pop();
                    if (op == '+') {

                        val = v2 + v1;
                    }else if (op == '-') {

                        val = v2 - v1;
                    }else if (op == '*') {

                        val = v2 * v1;
                    }else {

                        val = v2 / v1;
                    }
                    stack.push(val);
                }


                opStack.push(aChar);
            }
        }

        if (sb.length() > 0) {

            stack.push(Integer.valueOf(sb.toString()));
        }

        while (!opStack.isEmpty()) {

            int v1  = stack.pop();
            int v2 = stack.pop();
            int val = 0;
            Character op = opStack.pop();
            if (op == '+') {

                val = v2 + v1;
            }else if (op == '-') {

                val = v2 - v1;
            }else if (op == '*') {

                val = v2 * v1;
            }else {

                val = v2 / v1;
            }
            stack.push(val);
        }

        return stack.pop();
    }

    public static int calculate01(String s) {

        List<String> lists = new ArrayList<>();
        Stack<Character> stack = new Stack<>();


        char[] chars = s.toCharArray();

        StringBuilder sb = new StringBuilder();
        for (char aChar : chars) {

            if (aChar >= '0' && aChar <= '9') {

                sb.append(aChar);
            }else if(aChar == '+' || aChar == '-'|| aChar == '*' || aChar == '/') {

                lists.add(sb.toString());
                sb.delete(0,sb.length());

                while (!stack.isEmpty() && !((aChar == '*'  || aChar == '/') && (stack.peek() == '-' || stack.peek() == '+'))) {

                    lists.add(String.valueOf(stack.pop()));
                }

                stack.push(aChar);
            }

        }

        if (sb.length() > 0) {

            lists.add(sb.toString());
        }

        while (!stack.isEmpty()) {

            lists.add(String.valueOf(stack.pop()));
        }


        double res = 0.0;

        Stack<Integer> dStack = new Stack<>();

        for (String val : lists) {

            if (val.equals("+") || val.equals("-") || val.equals("*") || val.equals("/")) {

                int pv;
                int v1 = dStack.pop();
                int v2 = dStack.pop();

                if (val.equals("+")) {

                    pv = v1 + v2;
                }else if (val.equals("-")) {

                    pv = v2 - v1;
                }else if (val.equals("*")) {

                    pv = v1 * v2;
                }else {


                    pv = v2 / v1;
                }
                dStack.push(pv);
            }else {

                dStack.push(Integer.valueOf(val));
            }
        }

       return dStack.pop();
    }

    public static void main(String[] args) {

        String express = "  14/3*2";
//        String express = " 1-1+1 ";

        System.out.println(calculate(express));
    }

}
